<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="https://gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.96.0" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Go Swagger &middot; gofulljs blog</title>
  <meta name="description" content="" />

  
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/poole.css">
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="https://gosc.gitee.io/sc/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class=" ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="https://gosc.gitee.io/sc/"><h1>gofulljs blog</h1></a>
      <p class="lead">
       my all blog 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="https://gosc.gitee.io/sc/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2022. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Go Swagger</h1>
  <time datetime=2022-04-18T11:02:51&#43;0800 class="post-date">Mon, Apr 18, 2022</time>
  <h2 id="简单-gin-swagger">简单 gin swagger</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">// @Summary 测试SayHelloGin
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Description 说hello
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Tags 测试
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Accept json
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Param name query string true &#34;人名&#34; default(gin)
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Success 200 {string} string &#34;{&#34;msg&#34;: &#34;hello Razeen&#34;}&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Failure 400 {string} string &#34;{&#34;msg&#34;: &#34;who are you&#34;}&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Router /hello [get]
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">HandleHello</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">who</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;name&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">who</span> <span style="color:#f92672">==</span> <span style="color:#e6db74">&#34;&#34;</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusBadRequest</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{<span style="color:#e6db74">&#34;msg&#34;</span>: <span style="color:#e6db74">&#34;who are u?&#34;</span>})
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{<span style="color:#e6db74">&#34;msg&#34;</span>: <span style="color:#e6db74">&#34;hello &#34;</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">who</span>})
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>说明</p>
<pre tabindex="0"><code>注解	描述
@Summary	摘要
@Produce	API 可以产生的 MIME 类型的列表，MIME 类型你可以简单的理解为响应类型，例如：json、xml、html 等等
@Param	参数格式，从左到右分别为：参数名、入参类型、数据类型、是否必填、注释
@Success	响应成功，从左到右分别为：状态码、参数类型、数据类型、注释
@Failure	响应失败，从左到右分别为：状态码、参数类型、数据类型、注释
@Router	路由，从左到右分别为：路由地址，HTTP 方法
</code></pre><h2 id="参数映射为结构体">参数映射为结构体</h2>
<p>models 文件夹</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">models</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">TestAgeMode</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Age</span>  <span style="color:#66d9ef">int</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Api</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Code</span> <span style="color:#66d9ef">int</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Data</span> <span style="color:#66d9ef">interface</span>{}
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Msg</span>  <span style="color:#66d9ef">string</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>新增handler</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#75715e">// @Summary 测试
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Description 测试返回其他包model
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Tags 测试2
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Accept application/json
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Produce application/json
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Param object query TestParams false &#34;参数&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Success 200 {object} models.Api{data=models.TestAgeMode}
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">// @Router /test [get]
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">HandleTest</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">params</span> <span style="color:#a6e22e">TestParams</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Bind</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">params</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#ae81ff">200</span>, <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">Api</span>{
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Code</span>: <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>,
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Msg</span>:  <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Error</span>(),
</span></span><span style="display:flex;"><span>		})
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">age</span> <span style="color:#66d9ef">int</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">switch</span> <span style="color:#a6e22e">params</span>.<span style="color:#a6e22e">Name</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#e6db74">&#34;xiaowang&#34;</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">age</span> = <span style="color:#ae81ff">18</span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">case</span> <span style="color:#e6db74">&#34;xiaozhang&#34;</span>:
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">age</span> = <span style="color:#ae81ff">26</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">params</span>.<span style="color:#a6e22e">Add100</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">age</span> <span style="color:#f92672">+=</span> <span style="color:#ae81ff">100</span>
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#ae81ff">200</span>, <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">Api</span>{
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Code</span>: <span style="color:#ae81ff">200</span>,
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">Data</span>: <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">TestAgeMode</span>{
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Name</span>: <span style="color:#a6e22e">params</span>.<span style="color:#a6e22e">Name</span>,
</span></span><span style="display:flex;"><span>			<span style="color:#a6e22e">Age</span>:  <span style="color:#a6e22e">age</span>,
</span></span><span style="display:flex;"><span>		},
</span></span><span style="display:flex;"><span>	})
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div>
</div>


    </main>

    
      
    
  </body>
</html>
